package com.zmx.doctor.view.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.zmx.doctor.BaseActivity;
import com.zmx.doctor.R;
import com.zmx.doctor.okhttp.OkHttp3ClientManager;
import com.zmx.doctor.utils.AllUtil;
import com.zmx.doctor.view.util.MyDialog;
import com.zmx.doctor.view.util.MySharedPreferences;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 程序猿：胖胖祥
 * 时 间：2020/11/16  23:31
 * 功 能：创建团队
 */
public class CreateTeamActivity extends BaseActivity {

    @BindView(R.id.registered)
    ImageView registered;
    @BindView(R.id.edit_name)
    EditText editName;
    @BindView(R.id.edit_synopsis)
    EditText editSynopsis;
    @BindView(R.id.edit_price)
    EditText editPrice;
    @BindView(R.id.submit)
    Button submit;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_create_team;
    }

    @Override
    protected void initViews() {

        setToolbar(R.id.tool_bar);
        ButterKnife.bind(this);

    }


    @OnClick({R.id.registered, R.id.submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.registered:

                View view_layout = getLayoutInflater().inflate(R.layout.dialog_layout, null);
                Button know = view_layout.findViewById(R.id.know);

                MyDialog mMyDialog = new MyDialog(mActivity, 10, 10, view_layout, R.style.DialogTheme);
                mMyDialog.setCancelable(true);
                mMyDialog.show();
                know.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mMyDialog.cancel();
                    }
                });
                break;
            case R.id.submit:
                if (editName.getText().toString().equals("")){

                    Toast("请输入名称");
                    return;

                }
                if (editSynopsis.getText().toString().equals("")){

                    Toast("请输入简介");
                    return;

                }
                if (editPrice.getText().toString().equals("")){

                    Toast("请输入价格");
                    return;

                }
                getData();
                break;
        }
    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            switch (msg.what){

                case 1:

                    try {

                        JSONObject object = new JSONObject(msg.obj.toString());
                        Toast(object.getString("msg"));

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    break;

            }

        }
    };

    public void getData() {

        List<String> ls = new ArrayList<>();
        ls.add(editSynopsis.getText().toString());
        ls.add(editName.getText().toString());
        ls.add(editPrice.getText().toString());
        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(ls)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "create");
        params.put("data[name]", editName.getText().toString());
        params.put("data[describe]", editSynopsis.getText().toString());
        params.put("data[price]", editPrice.getText().toString());
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/admin/api.team/entry", params, handler, 1, 404);

    }

}
